//
//  EditGymAdminController.m
//  YunDongAdmin
//
//  Created by lqq on 2018/9/12.
//  Copyright © 2018年 cloudfighting.com. All rights reserved.
//


typedef NS_ENUM(NSInteger, EditGymAdminVCCell) {
    EditGymAdminVCCell_Account,//账号
    EditGymAdminVCCell_PWD,//密码
    EditGymAdminVCCell_Name,//姓名
    EditGymAdminVCCell_Sex,//性别
    EditGymAdminVCCell_Title,//职位
    EditGymAdminVCCell_Mobile,//联系电话
    EditGymAdminVCCell_Gym,//所在门店
};

#import "EditGymAdminController.h"
#import "CommonTableViewCell.h"
#import "I_Admin.h"



@interface EditGymAdminController ()<UITableViewDelegate,UITableViewDataSource,YDSwitchDelegate>
@property (nonatomic, strong) M_Admin *admin;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *deleteBtn;
@property (nonatomic, strong) NSArray *dataSource;

@property (nonatomic, strong) EditGymAdminResetPWCCell *resetPWDCell;
@property (nonatomic, strong) EditGymAdminSexCell *sexCell;
@end

@implementation EditGymAdminController
#pragma mark Class Method
+ (instancetype)controllerWithAdmin:(M_Admin *)admin
{
    EditGymAdminController *vc = [UIStoryboard controllerWithStoryBoardIdentify:NSStringFromClass([EditGymAdminController class]) inStoryBoard:@"Group"];
    vc.admin = admin;
    return vc;
}

#pragma mark Setter
- (EditGymAdminResetPWCCell *)resetPWDCell
{
    if (_resetPWDCell == nil) {
        _resetPWDCell = [self.tableView dequeueReusableCellWithIdentifier:NSStringFromClass([EditGymAdminResetPWCCell class])];
        [_resetPWDCell.resetBtn addTarget:self action:@selector(resetPwd) forControlEvents:UIControlEventTouchUpInside];
    }
    return _resetPWDCell;
}


- (EditGymAdminSexCell *)sexCell
{
    if (_sexCell == nil) {
        _sexCell = [self.tableView dequeueReusableCellWithIdentifier:NSStringFromClass([EditGymAdminSexCell class])];
        _sexCell.sexSwitchView.titles = @[@"男",@"女"];
        _sexCell.sexSwitchView.delegate = self;
    }
    return _sexCell;
}

#pragma mark Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"店长信息";
    _dataSource = @[@[@(EditGymAdminVCCell_Account),@(EditGymAdminVCCell_PWD)],@[@(EditGymAdminVCCell_Name),@(EditGymAdminVCCell_Sex),@(EditGymAdminVCCell_Title),@(EditGymAdminVCCell_Mobile),@(EditGymAdminVCCell_Gym)]];
    [self configUI];
    [self requestAdminDetail];
}

#pragma mark UI
- (void)configUI
{
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = TP.controllerBackColor;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[CommonTableViewCell class] forCellReuseIdentifier:NSStringFromClass([CommonTableViewCell class])];
    _deleteBtn.layer.cornerRadius = 2;
    _deleteBtn.layer.masksToBounds = YES;
    
}


#pragma mark NET
- (void)updateAdmin
{
    WeakSelf(self);
    [I_Admin updateAdminWithAdmin:_admin success:^{
        [weakself.tableView reloadData];
    } failure:^(NSError *error) {
        [AV showError:error];
    }];
}

- (void)requestAdminDetail
{
    WeakSelf(self);
    [I_Admin requestAdminDetailWithAdminId:_admin.accountId success:^(M_Admin *admin) {
        weakself.admin = admin;
        [weakself.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark Action

/**
 删除教练
 */
- (IBAction)deleteBtnClicked:(id)sender {
    WeakSelf(self);
    [YDAlertViewController alertViewControllerWithMessage:@"确定删除该店长吗？" cancleTitle:@"取消" confimTitle:@"删除" cancleBlock:nil confimBlock:^{
        [I_Admin deleteAdminWithAdminId:weakself.admin.accountId success:^{
            [AV showSuccess:@"删除成功"];
            [weakself.navigationController popViewControllerAnimated:YES];
        } failure:^(NSError *error) {
            [AV showError:error];
        }];
    }];
}




/**
 重置密码
 */
- (void)resetPwd
{
    [I_Admin resetPasswordWithAdminId:_admin.accountId adminType:_admin.roleId success:^{
        [AV showSuccess:@"重置成功\n密码为：888888"];
    } failure:^(NSError *error) {
        [AV showError:error];
    }];
}
#pragma mark YDSwitchDelegate
- (void)choiceIndex:(NSInteger)index
{
    if (index == 0) {
        _admin.userSex = AdminSexType_Male;
    } else {
        _admin.userSex = AdminSexType_Female;
    }
    [self updateAdmin];
}



#pragma mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr = _dataSource[section];
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CommonTableViewCell class])];
    EditGymAdminVCCell cellType = [_dataSource[indexPath.section][indexPath.row] integerValue];
    switch (cellType) {
        case EditGymAdminVCCell_Account://账号
        {
            cell.showStyle = CommonTableViewShowStyleRightText;
            cell.leftLabel.text = @"账号";
            cell.rightLabel.text = _admin.account;
        }
            break;
        case EditGymAdminVCCell_PWD://密码
        {
            return self.resetPWDCell;
        }
            break;
        case EditGymAdminVCCell_Name://姓名
        {
            cell.showStyle = CommonTableViewShowStyleArrow_RightText;
            cell.leftLabel.text = @"姓名";
            cell.rightLabel.text = _admin.userName;
        }
            break;
        case EditGymAdminVCCell_Sex://性别
        {
            self.sexCell.sexSwitchView.index = (_admin.userSex == AdminSexType_Female) ? 1 : 0;
            return self.sexCell;
        }
            break;
        case EditGymAdminVCCell_Title://职位
        {
            cell.showStyle = CommonTableViewShowStyleRightText;
            cell.leftLabel.text = @"职位";
            cell.rightLabel.text = _admin.userTitle;
        }
            break;
        case EditGymAdminVCCell_Mobile://联系电话
        {
            cell.showStyle = CommonTableViewShowStyleArrow_RightText;
            cell.leftLabel.text = @"联系电话";
            cell.rightLabel.text = _admin.userMobile;
        }
            break;
        case EditGymAdminVCCell_Gym://所在门店
        {
            cell.showStyle = CommonTableViewShowStyleRightText;
            cell.leftLabel.text = @"所在门店";
            cell.rightLabel.text = _admin.gym.gymName;
        }
            break;
            
        default:
            break;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    EditGymAdminVCCell cellType = [_dataSource[indexPath.section][indexPath.row] integerValue];
    switch (cellType) {
        case EditGymAdminVCCell_Account://账号
        {
     
        }
            break;
        case EditGymAdminVCCell_PWD://密码
        {
            
        }
            break;
        case EditGymAdminVCCell_Name://姓名
        {
            EditInfoController *editVC = [EditInfoController controllerWithEditType:EditInfoType_AdminName originStr:_admin.userName];
            WeakSelf(self);
            editVC.editBlock = ^(NSString *editStr) {
                weakself.admin.userName = editStr;
                [weakself updateAdmin];
            };
            [self.navigationController pushViewController:editVC animated:YES];
        }
            break;
        case EditGymAdminVCCell_Sex://性别
        {
            
        }
            break;
        case EditGymAdminVCCell_Title://职位
        {
            
        }
            break;
        case EditGymAdminVCCell_Mobile://联系电话
        {
            EditInfoController *editVC = [EditInfoController controllerWithEditType:EditInfoType_AdminMobile originStr:_admin.userMobile];
            WeakSelf(self);
            editVC.editBlock = ^(NSString *editStr) {
                weakself.admin.userMobile = editStr;
                [weakself updateAdmin];
            };
            [self.navigationController pushViewController:editVC animated:YES];
        }
            break;
        case EditGymAdminVCCell_Gym://所在门店
        {
            
        }
            break;
            
        default:
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 48;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *view = [[UIView alloc] init];
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"账号信息";
        titleLabel.textColor = HEXColor(@"4A4A4A");
        titleLabel.font = RegularFont(15);
        [view addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view).offset(20);
            make.bottom.equalTo(view).offset(-5);
            make.height.equalTo(@(21));
        }];
        return view;
    } else {
        UIView *view = [[UIView alloc] init];
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"成员信息";
        titleLabel.textColor = HEXColor(@"4A4A4A");
        titleLabel.font = RegularFont(15);
        [view addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view).offset(20);
            make.bottom.equalTo(view).offset(-5);
            make.height.equalTo(@(21));
        }];
        return view;
    }
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}
@end


@implementation EditGymAdminResetPWCCell

@end


@implementation EditGymAdminSexCell


@end
